package com.hqyj.springCloudTest.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.entity.common.ResultEntity;
import com.hqyj.entity.common.SearchBean;
import com.hqyj.entity.test.City;
import com.hqyj.springCloudTest.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//代表该类的方法都是数据没有页面的渲染
@RestController //相当于@RequestBody + @Controller
@RequestMapping("/api/test")
public class CityController {

    @Autowired
    private CityService cityService;

    /**
     *127.0.0.1:8761/api/test/city ---- post
     * {"cityName":"baiyun","localCityName":"义马","countryId":522}
     */
    @PostMapping(value = "/city",consumes = "application/json")
    //                                  接受前端传过来的City数据
    public ResultEntity<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }


    /**
     * 127.0.0.1:8761/api/test/city ---- put
     * cityId=2258,cityName=dreamCity,localCityName=天国,
     * countryId=522,district=sichuan,population=11111111111
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    //这个方法用form表单接受  用@ModelAttribute 来 接受表单数据
    public ResultEntity<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1:8761/api/test/city/2258 ---- delete
     */
    @DeleteMapping("/city/{cityId}")
    public ResultEntity<Object> updateCity(@PathVariable int cityId){
        return cityService.deleteCity(cityId);
    }

    /**
     * 联表查询
     * 127.0.0.1:8761/api/test/city/2257 ---- get
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId){
        return cityService.getCityByCityId(cityId);
    }

    /**
     * 查询出一个国家下所有的城市
     * 127.0.0.1:8761/api/test/cities/522 ---- get
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCityByCountryId(@PathVariable int countryId){
        return cityService.getCityByCountryId(countryId);
    }

    /**
     * 127.0.0.1:8761/api/test/cities ---- post
     * {"currentPage":1, "pageSize":5, "keyWord":"shanghai", "orderBy":"city_id",
     * "direction":"asc"}
     */
    @PostMapping(value = "/cities",consumes = "application/json")
    public PageInfo<City> getCityBySearchBean(@RequestBody SearchBean searchBean){
        return cityService.getCityBySearchBean(searchBean);
    }
}
